/**
 * Copyright 2011 Sibaozuan(superskysbz@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package summary.aidl.client;

import summary.aidl.server.IAIDLServerService;
import summary.android.activity.R;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * 
 * @author Sibaozuan(superskysbz@gmail.com)
 * 
 */
public class AidlClientActivity extends Activity {
	private TextView mTextView;
	private Button mButton;
	private IAIDLServerService mIaidlServerService = null;
	private ServiceConnection mConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mIaidlServerService = IAIDLServerService.Stub.asInterface(service);
			// aidl通信
			try {
				String mText = "Say hello: " + mIaidlServerService.sayHello() + "/n";
				mText += "书名: " + mIaidlServerService.getBook().getBookName() + "/n";
				mText += "价格: " + mIaidlServerService.getBook().getBookPrice();
				mTextView.setText(mText);
			} catch (RemoteException e) {
				e.printStackTrace();
			}

		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mIaidlServerService = null;
		}

	};

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.aidl_test);
		// 初始化控件
		mTextView = (TextView) findViewById(R.id.aidl_text_view);
		mButton = (Button) findViewById(R.id.aidl_button);
		// 增加事件响应
		mButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// bindService
				Intent service = new Intent("com.chapter8.aidl.IAIDLServerService");
				bindService(service, mConnection, BIND_AUTO_CREATE);
			}

		});
	};
}
